<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version

	$Source$
	$Revision$
	$Author$
	$Date$
**********************************************/

abstract class Poodle_PHP_INI
{
	private static
		$vars = null,
		$set  = false;

	public static function get($var, $def=null)
	{
		if (!is_array(self::$vars)) { return $def; }
		# get_cfg_var() returns the value from php.ini, while ini_get() returns the runtime value
		return isset(self::$vars[$var]) ? self::$vars[$var] : ini_get($var);
	}

	public static function set($var, $val)
	{
		if (self::$set) { return ini_set($var, $val); }
		$r = self::get($var);
		self::$vars[$var] = $val;
		return $r;
	}

	public static function enabled($var, $or_null=true)
	{
		$var = self::get($var);
		return (!empty($var) || (is_null($var) && $or_null));
	}

	public static function init()
	{
		//if (empty($_COOKIE['PoodleTimezone']) || !date_default_timezone_set($_COOKIE['PoodleTimezone']))
		//{
		//	unset($_COOKIE['PoodleTimezone']);
		//	date_default_timezone_set('UTC');
		//}
		$err = error_reporting(0);
//		register_shutdown_function('umask', umask(0)); # Reset at shutdown
		# php.net/manual/en/outcontrol.configuration.php#ini.output-buffering
		Poodle::ob_clean();
		# php.net/manual/en/outcontrol.configuration.php#ini.implicit-flush
		//ob_implicit_flush(0);
		if (function_exists('ini_get')) {
			self::$vars = array();
		} else {
			if (!function_exists('ini_get_all')) { return; }
			self::$vars = ini_get_all();
			foreach (self::$vars as &$v) { $v = $v['local_value']; }
		/*
			PHP_INI_USER    1 Entry can be set in user scripts
			PHP_INI_PERDIR  2 Entry can be set in php.ini, .htaccess or httpd.conf
			PHP_INI_SYSTEM  4 Entry can be set in php.ini or httpd.conf
			PHP_INI_??????  8
			PHP_INI_?????? 16
			PHP_INI_?????? 32
			PHP_INI_ALL    63
		*/
		}
		$fl = self::get('disable_functions');
		self::$set = (!is_null($fl) && false === stripos($fl, 'ini_set'));
		if (self::$set)
		{
			# Modify PHP ini to prevent:
//			ini_set('display_errors', 1);
			ini_set('html_errors', 0);
//			ini_set('error_log', Poodle::$DIR_BASE.'cache/php-errors.'.date('o-W').'.log');
//			ini_set('log_errors', 0);
			ini_set('log_errors_max_len', 0); # prevent half error messages

			ini_set('implicit_flush', 0); # direct output
//			ini_set('output_buffering', 0); # failing Poodle_ob_handler
			ini_set('zend.ze1_compatibility_mode', 0); # __clone bug
			ini_set('zlib.output_compression', 0); # double compression
		}
		if (version_compare(phpversion(), '6', '<')
		 && (get_magic_quotes_gpc() || self::enabled('magic_quotes_sybase') || self::enabled('register_globals') || self::enabled('register_long_arrays')))
		{
			require(dirname(__FILE__).'/compat_php6.php');
		}

		// fix include_path
		$dirs = explode(PATH_SEPARATOR, get_include_path());
		foreach ($dirs as $i => $dir) {
			$dir = rtrim($dir,DIRECTORY_SEPARATOR);
			if (is_dir($dir)) {
				$dirs[$i] = $dir;
			} else {
				// directory doesn't exist and may cause errors
				// when open_basedir is configured
				unset($dirs[$i]);
			}
		}
		set_include_path(implode(PATH_SEPARATOR, $dirs));

		error_reporting($err);

		if (!function_exists('mb_internal_encoding')) { Poodle_Unicode_MB::init(); }
		mb_internal_encoding(Poodle::CHARSET);
		if (function_exists('iconv_set_encoding')) {
			iconv_set_encoding('input_encoding',    Poodle::CHARSET);
			iconv_set_encoding('internal_encoding', Poodle::CHARSET);
			iconv_set_encoding('output_encoding',   Poodle::CHARSET);
		}
	}
//	ini_restore($var)
}
